package com.example.accessibilitydemo.service;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.example.accessibilitydemo.util.PreferenceUtil;

/**
 * 朋友圈点赞
 * 
 * @author chencq
 *
 */
public class WeChatAddLikeService extends BaseAccessibilityService {

	private static final String TAG = WeChatAddLikeService.class.getSimpleName();
	private int num = 10;
	private boolean addlike = false;
	private int count = 0;// 记录已点赞的人数

	List<AccessibilityNodeInfo> fuBtns = new ArrayList<AccessibilityNodeInfo>();

	@Override
	public void onAccessibilityEvent(AccessibilityEvent event) {
		super.onAccessibilityEvent(event);
		try {
			addlike = PreferenceUtil.getBoolean("addlike", false);
			num = Integer.parseInt(PreferenceUtil.getString("add_like_num", "10"));

			Log.i(TAG, "addlike: " + addlike + ",num:" + num);

			if (addlike == false)
				return;

			int eventType = event.getEventType();
			if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
					&& event.getClassName().equals("com.tencent.mm.ui.LauncherUI")) {// 微信打开
				// 当前在微信聊天页就点开"发现"
				clickViewByText("发现");
				// 点开"朋友圈"
				delayClickViewByText("朋友圈", 1000);
			} else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
					&& event.getClassName().equals("com.tencent.mm.plugin.sns.ui.SnsTimeLineUI")) {// 进入朋友圈

				addLike(getRootInActiveWindow());
			}
		} catch (Exception ex) {
			Log.i(TAG, "throw exception:" + ex);
			PreferenceUtil.commitBoolean("addlike", false);
			count = 0;
			performHomeClick();
		}
	}

	public void addLike(AccessibilityNodeInfo rootNode) {
		// 获取点赞区域
		fuBtns = findAllViewByID("com.tencent.mm:id/co0");
		Log.i(TAG, "fuBtns数量:" + fuBtns.size() + ",count:" + count);

		// 达到需要点赞数目，点赞任务完成
		if (count >= num) {
			PreferenceUtil.commitBoolean("addlike", false);
			count = 0;
			performHomeClick();
		}

		if (fuBtns.size() > 0) {
			for (int k = 0; k < fuBtns.size(); k++) {
				List<AccessibilityNodeInfo> plns = fuBtns.get(k).findAccessibilityNodeInfosByViewId(
						"com.tencent.mm:id/cj9");
				Log.i(TAG, "plns数量:" + fuBtns.size());
				if (plns != null && !plns.isEmpty()) {
					AccessibilityNodeInfo pln = plns.get(0);
					performViewClick(pln);

					sleep(1000);
					AccessibilityNodeInfo like = findView("com.tencent.mm:id/cj5", "赞");
					if (like != null) {
						performViewClick(like);
						count++;
					}
				}
			}

		}

		for (int k = 0; k < rootNode.getChild(0).getChildCount(); k++) {
			if (rootNode.getChild(0).getChild(k).getClassName().equals("android.widget.ListView")) {
				AccessibilityNodeInfo node_lsv = rootNode.getChild(0).getChild(k);
				node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
				Log.i(TAG, "向上滑动.......");
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException mE) {
							mE.printStackTrace();
						}
						addLike(getRootInActiveWindow());
					}
				}).start();
			}
		}
	}

}
